package com.renwei.yunlong.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.me.ObjectInformationtActivity;
import com.renwei.yunlong.adapter.NewDetailsRelateAdapter;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.NewDetailsRelatedBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

import java.util.HashMap;
import java.util.List;

import butterknife.Bind;


/**
 * 对象详情  相关  页面
 * <p>
 * Created by Administrator on 2017/9/7.
 */

public class ObjectRelatedFragment extends BaseLazyFragment implements HttpTaskListener, BaseRecyclerViewAdapter.OnItemClickLitener {


    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    private NewDetailsRelateAdapter adapter;
    private String outSourceFlag;
    private String assetId;

    public ObjectRelatedFragment(String outSourceFlag, String assetId) {
        this.outSourceFlag = outSourceFlag;
        this.assetId = assetId;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_new_details_related;
    }

    @Override
    public void onLazyLoad() {
        initView();
        initData();
    }

    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new NewDetailsRelateAdapter(getActivity());
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);
    }


    public void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {//业主
            if ("0".equals(outSourceFlag)) {
                map.put("currentUserId", ownerBean.getRows().getEmployeeId());
            }
        } else if ("2".equals(companyType)) {//服务商
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
        }
        map.put("assetId", assetId);
        map.put("page", "1");
        map.put("rows", "100000");
        ServiceRequestManager.getManager().getObjectRelated(getContext(), outSourceFlag, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onItemClick(View view, int position) {
        NewDetailsRelatedBean.Rows bean = adapter.getItem(position);
        ObjectInformationtActivity.OpenAcitivity(getContext(),"对象详情",outSourceFlag,bean.getAssetId(),bean.getAssetName());
    }

    //Adapter数据源
    private void setDataList(List<NewDetailsRelatedBean.Rows> mList) {
        if (mList.size() == 0) {
            stateLayout.showEmptyView();
        } else {
            stateLayout.showContentView();
        }
        adapter.setData(mList);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        NewDetailsRelatedBean ownerBean = new Gson().fromJson(result, NewDetailsRelatedBean.class);
        if (ownerBean.getMessage().getCode() == 200) {
            setDataList(ownerBean.getRows());
        }
    }

    @Override    public void onException(int requestId, String code) {        showTopWrongMsg(getContext().getResources().getString(R.string.net_work_error));    }
}
